package service.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import service.edu.entity.Course;
import service.edu.entity.vo.CourseInfo;
import service.edu.entity.vo.CourseQuery;
import service.edu.entity.vo.CoursePublish;
import service.edu.service.CourseService;
import service.edu.result.R;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-08-05
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation("添加课程")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@ApiParam(name = "courseInfo",value = "添加课程的基本信息")
                                @RequestBody(required = false) CourseInfo courseInfo){
        String id=courseService.saveCourseInfo(courseInfo);
        return R.Ok().data("courseId", id);
    }

    @ApiOperation("根据课程id查询课程基本信息")
    @GetMapping("/queryCourse/{courseId}")
    public R queryCourseById(@PathVariable("courseId")String courseId){
          CourseInfo courseInfo=courseService.getCourseInfo(courseId);
          return R.Ok().data("courseInfo",courseInfo);
    }

    @ApiOperation("根据课程id修改课程信息")
    @PostMapping("/updateCourse")
    public R updateCourse(@ApiParam(name = "courseInfo",value = "修改的课程的基本信息")
                             @RequestBody(required = false)CourseInfo courseInfo){
        courseService.updateCourseInfo(courseInfo);
        String courseId=courseInfo.getId();
        return R.Ok().data("courseId", courseId);
    }

    @ApiOperation("根据课程id进行复杂查询")
    @GetMapping("getCoursePublish/{courseId}")
    public R getCoursePublishInfo(@ApiParam(name = "courseId",value = "课程id")
                                   @PathVariable("courseId")String courseId){
         CoursePublish coursePublish=courseService.getCoursePublishInfo(courseId);
         return R.Ok().data("coursePublish",coursePublish);
    }

    @ApiOperation("根据课程id进行发布课程")
    @PostMapping("publishCourse/{courseId}")
    public R publishCourseById(@ApiParam(name = "courseId",value = "课程id")
                                   @PathVariable("courseId")String courseId){
        Course course = new Course();
        course.setId(courseId);//确定课程
        course.setStatus("Normal");//Normal表示课程是已发布状态,Draft为未发布状态
        courseService.updateById(course);
        return R.Ok();
    }

    @ApiOperation("查询所有课程")
    @GetMapping("/queryAllCourses")
    public R queryAllCourses(){
        List<Course> courseList = courseService.list(null);
        return R.Ok().data("courseList", courseList);
    }

    /**分页查询课程
     * current:当前页
     * limit:每页记录数
     */
    @ApiOperation("分页查询课程")
    @GetMapping("/query/course/{current}/{limit}")
    public R queryByPage(@ApiParam(name = "current", value = "当前页码", required = true)
                         @PathVariable("current")long current,
                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                         @PathVariable("limit")long limit){
        Page<Course> page = new Page<>(current,limit);
        courseService.page(page, null);//分页查询
        long total=page.getTotal();//总记录数
        List<Course> records = page.getRecords();//数据list集合
        return R.Ok().data("total", total).data("courses",records);
    }

    /**分页条件查询
     * current:当前页
     * limit:每页记录数
     * CourseQuery:教师查询条件封装类
     * 加了requestBody的参数需要以json字符串的形式传递数据
     */
    @ApiOperation("分页条件查询课程")
    @PostMapping("/queryCourse/page/condition/{current}/{limit}")
    public R queryByConditionAndPage(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current")long current,
                                     @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit")long limit,
                                     @ApiParam(name = "courseListVo",value = "查询的条件对象") @RequestBody(required = false) CourseQuery courseQuery)
    {
        Page<Course> page = new Page<>(current,limit);
        //进行条件查询
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();//课程名称
        String price = courseQuery.getPrice();//课程价格
        String status = courseQuery.getStatus();//课程是否发布
        String begin = courseQuery.getBegin();//课程创建时的时间
        String end = courseQuery.getEnd();//课程修改时的时间

        //条件查询是根据数据库字段来鉴别的
        if (!StringUtils.isEmpty(title))
            queryWrapper.like("title", title);
        if (!StringUtils.isEmpty(price))
            queryWrapper.le("price", price);
        if (!StringUtils.isEmpty(status))
            queryWrapper.eq("status", status);
        if (!StringUtils.isEmpty(begin))
            queryWrapper.ge("gmt_Create", begin);
        if (!StringUtils.isEmpty(end))
            queryWrapper.le("gmt_Create", end);
        //排序
        queryWrapper.orderByAsc("gmt_create");
        //开始分页查询
        courseService.page(page, queryWrapper);
        //先进行条件查询后再返回分页查询的结果
        long total = page.getTotal();//总记录数
        List<Course> courses = page.getRecords();//数据list集合
        //返回数据
        return R.Ok().data("total", total).data("courses",courses);
    }

    @ApiOperation("根据课程id删除课程")
    @DeleteMapping("deleteCourse/{courseId}")
    public R deleteCourseById(@ApiParam(name = "courseId",value = "课程id")
                              @PathVariable("courseId")String courseId){
            courseService.removeCourse(courseId);
            return R.Ok();
    }
}

